package com.sy.server.util;

import com.sy.server.dto.CostWithoutDetail;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {
    public static void write(String filePath, List<CostWithoutDetail> list){
        File dbfFile = new File(filePath);
        OutputStream os = null;
        WritableWorkbook wwb=null;
        try{
            os=new FileOutputStream(dbfFile);
            if (!dbfFile.exists() || dbfFile.isDirectory()) {
                dbfFile.createNewFile();
            }
            wwb = Workbook.createWorkbook(dbfFile);
            WritableSheet ws = wwb.createSheet("Sheet 1", 0);
            ws.addCell(new Label(0,0,"序号"));
            ws.addCell(new Label(1,0,"日期"));
            ws.addCell(new Label(2,0,"餐饮支出"));
            ws.addCell(new Label(3,0,"住房支出"));
            ws.addCell(new Label(4,0,"通讯支出"));
            ws.addCell(new Label(5,0,"交通支出"));
            ws.addCell(new Label(6,0,"日常支出"));
            ws.addCell(new Label(7,0,"学习支出"));
            ws.addCell(new Label(8,0,"服饰支出"));
            ws.addCell(new Label(9,0,"零食支出"));
            ws.addCell(new Label(10,0,"水果支出"));
            ws.addCell(new Label(11,0,"医疗支出"));
            ws.addCell(new Label(12,0,"其它支出"));
            ws.addCell(new Label(13,0,"支出总和"));
            int index=0;
            for(CostWithoutDetail cwd:list){
                ws.addCell(new Label(0,index+1,String.valueOf(index+1)));
                ws.addCell(new Label(1,index+1,cwd.getDay()));
                ws.addCell(new Label(2,index+1,cwd.getEat().toString()));
                ws.addCell(new Label(3,index+1,cwd.getHouse().toString()));
                ws.addCell(new Label(4,index+1,cwd.getCommunication().toString()));
                ws.addCell(new Label(5,index+1,cwd.getTraffic().toString()));
                ws.addCell(new Label(6,index+1,cwd.getDaily().toString()));
                ws.addCell(new Label(7,index+1,cwd.getEducation().toString()));
                ws.addCell(new Label(8,index+1,cwd.getClothes().toString()));
                ws.addCell(new Label(9,index+1,cwd.getSnacks().toString()));
                ws.addCell(new Label(10,index+1,cwd.getFruits().toString()));
                ws.addCell(new Label(11,index+1,cwd.getMedical().toString()));
                ws.addCell(new Label(12,index+1,cwd.getOthers().toString()));
                ws.addCell(new Label(13,index+1,cwd.getSum().toString()));
                index++;
            }
            wwb.write();
            os.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(wwb!=null){
                try {
                    wwb.close();
                } catch (IOException | WriteException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        List<CostWithoutDetail> list=new ArrayList<>();
        CostWithoutDetail cwd=new CostWithoutDetail();
        cwd.setDay("2020-04-12");
        cwd.setFruits(12.0);
        cwd.setEat(11.0);
        CostWithoutDetail cwd1=new CostWithoutDetail();
        cwd.setDay("2020-04-11");
        cwd.setFruits(13.0);
        cwd.setEat(11.0);
        cwd.setCommunication(1.3);
        list.add(cwd);
        list.add(cwd1);
        try {
            write("excel.xlsx",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
